package view.overlay;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

public class MenuOverlay extends PassiveOverlay {
	
	// The list of command buttons
	private List<Button> mCommandButtons;
	
	// Padding from the buttons
	private int mButtonPaddingTop;
	private int mButtonPaddingLeft;
	
	// Size of the buttons
	private int mButtonWidth;
	private int mButtonHeight;
	
	private Orientation mOrientation;
	
	private Dimension mPreferredSize;
	

	
	
	public MenuOverlay() {
		mCommandButtons = new ArrayList<Button>();
		
		mPreferredSize = new Dimension(0, 0);
		
		mOrientation = Orientation.VERTICAL;
		
		
		mButtonWidth = 100;
		mButtonHeight = 50;
		
		mButtonPaddingTop = 20;
		mButtonPaddingLeft = 10;
			
	}
	
	
	public void addMenuOption(String option, MouseListener listener) {
		Button button = new Button(option);
		button.setListener(listener);
		mCommandButtons.add(button);
		
		recalculateBounds();
		
		
		this.invalidate();
	}
	
	public void removeMenuOption(String option) {
		for (Button btn : mCommandButtons) {
			if (btn.getText().equals(option)) {
				mCommandButtons.remove(btn);
			}
		}
		
		recalculateBounds();
		
		this.invalidate();
	}
	
	@Override
	public void onMouseClicked(MouseEvent e) {
		
		for (Button b : mCommandButtons) {
			
			if (b != null) {
				if (b.contains(e.getPoint())) {
					b.activate(e);
				}
			}
		}
	}

	@Override
	public void onMouseMoved(MouseEvent e) {
		for (Button button : mCommandButtons) {
			
			if (button == null) continue;
			
			Rectangle r = button.getBounds();
			
			if (r != null) {
				if (r.contains(e.getPoint())) {
					button.setHighlighted(true);
				}
				else {
					button.setHighlighted(false);
				}
			}
		}
	}
	
	@Override
	public void onMousePressed(MouseEvent e) {
		for (Button btn : mCommandButtons) {
			
			if (btn == null) continue;
			
			if (btn.contains(e.getPoint())) {
				
				btn.setDepressed(true);
				
				this.invalidate();
				return;
			}
		}
	}
	
	@Override
	public void onMouseReleased(MouseEvent e) {
		
		for (Button btn : mCommandButtons) {
			
			if (btn == null) continue;
			
			btn.setHighlighted(false);
			btn.setDepressed(false);
			
		}
		
		this.invalidate();
	}
	
	public void setOrientation(Orientation o) {
		this.mOrientation = o;
		
		this.invalidate();
	}
	
	public Orientation getOrientation() {
		return (this.mOrientation);
	}

	@Override
	protected void paint(Graphics g) {
		for (Button button : mCommandButtons) {
			
			if (button == null) continue;
			button.draw(g);
		}
		
	}

	@Override
	public Dimension getPreferredSize() {
		return (mPreferredSize);
	}
	
	
	private void recalculateBounds() {
		int xOffset = mButtonPaddingLeft;
		int yOffset = mButtonPaddingTop;
		
		// Recalculate the button bounds
		for (Button b : mCommandButtons) {

			if (b == null) continue;
			
			Rectangle r = b.getBounds();
			
			if (r == null) {
				r = new Rectangle(0, 0, 0, 0);
			}
			
			r.setLocation(xOffset, yOffset);
			
			
			r.setSize(mButtonWidth, mButtonHeight);
			
			if (mOrientation == Orientation.VERTICAL) {
				// vertical, so increment y
				yOffset += mButtonHeight + mButtonPaddingTop;
			}
			else {
				// horizontal, so increment x
				xOffset += mButtonWidth + mButtonPaddingLeft;
			}
			
			b.setBounds(r);
			
		}
		
		
		if (mOrientation == Orientation.VERTICAL) {
			// vertical
			mPreferredSize.setSize(mButtonWidth + 2 * mButtonPaddingLeft,
									mButtonHeight * mCommandButtons.size() + 2 * mButtonPaddingTop);
		}
		else {
			// horizontal
			mPreferredSize.setSize(mButtonWidth * mCommandButtons.size() + 2 * mButtonPaddingLeft,
									mButtonHeight + 2 * mButtonPaddingTop);
		}
	}
	
	
	enum Orientation {
		VERTICAL,
		HORIZONTAL
	}
	
	
}
